package de.fhin.mas.util;

/**
 * 
 * @author Matthias Wilhelm
 * @author Thomas Schlaefer
 * 
 */
public class RelativeCoordinate implements Comparable<RelativeCoordinate> {

	private int angle = 0;
	private int distance = 0;

	public RelativeCoordinate(int angle, int distance) {
		this.angle = angle;
		this.distance = distance;
		check();
	}

	public RelativeCoordinate get_RelativeCoordinate() {
		return this;
	}

	public void set_RelativeCoordinate(int angle, int distance) {
		this.angle = angle;
		this.distance = distance;
		check();
	}

	public void set_RelativeCoordinate(RelativeCoordinate relativeCoordinate) {
		this.angle = relativeCoordinate.angle;
		this.distance = relativeCoordinate.distance;
	}

	public void setAngle(int angle) {
		this.angle = angle;
		check();
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

	public void forwardOnly() {
		if (distance < 0) {
			distance = -distance;
			angle = angle - 180;
			check();
		}
	}

	/**
	 * Gibt den Winkel der Koordinate zurück
	 * 
	 * @return Der Winkel in Grad
	 */
	public int getAngle() {
		return angle;
	}

	/**
	 * Gibt den Entfernungswert der Koordinate zurück
	 * 
	 * @return Die Entfernung in Zentimetern
	 */
	public int getDistance() {
		return distance;
	}

	public Coordinate getCoordinate(int angle) {
		angle = ((this.angle + 180) + angle) % 360;
		int x = (int) Math.round(Math.sin(angle) * this.distance);
		int y = (int) Math.round(Math.cos(angle) * this.distance);
		return new Coordinate(x, y);

	}

	/**
	 * Überprüft ob angle zwischen -180 und 179
	 */
	private void check() {
		this.angle = (this.angle + 180) % 360 - 180;
	}

	public void add(RelativeCoordinate relativeCoordinate) {
		this.angle = this.angle + relativeCoordinate.angle;
		this.distance = this.distance + relativeCoordinate.distance;
		check();
	}

	public void addAngle(int angle) {
		this.angle += angle;
	}

	public void addDistance(int distance) {
		this.distance += distance;
	}

	public void println() {
		System.out.println(angle + "° " + distance + "cm");
	}

	@Override
	public String toString() {
		return angle + "° " + distance + "cm";
	}

	/**
	 * notwendig, damit zwei relative Koordinaten anhand ihres Winkels
	 * miteinander verglichen werden können
	 */
	public int compareTo(RelativeCoordinate c) {

		if (this.angle == c.getAngle()) {
			return 0;
		} else if (this.angle > c.getAngle()) {
			return 1;
		} else {
			return -1;
		}
	}
}
